﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BLL;
using DAL;

namespace TTCN_QLDiemC3.MyUserControl.UCHienThi
{
    public partial class UCSuaGiaoVien : UserControl
    {
        GiaoVienBUO gvBUO = new GiaoVienBUO();
        NumberBUO numberBUO = new NumberBUO();
        MonHocBUO mhBUO = new MonHocBUO();

        int id;
        String id_GV, md;

        public UCSuaGiaoVien(String id_GV, String id_Khoi, String id_MH)
        {
            InitializeComponent();
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = gvBUO.HienThiGiaoVien();
            showKhoi();
            md = id_MH;

            lbl_idGV.Text = id_GV.ToString();

            tbl_GiaoVien gv = gvBUO.GetSingle(lbl_idGV.Text);
            txtDiaChi.Text = gv.DiaChi;
            txtEmail.Text = gv.Email;
            txtSDT.Text = gv.SDT;
            txtTenGV.Text = gv.HoTenGV;
            dateTimePicker1.Text = gv.NgaySinh;

            cbxMonHoc.Text = mhBUO.getMonHoc(id_MH);
            cbxKhoi.Text = gvBUO.getKhoi(id_Khoi);
        }

        private void btnLamLai_Click(object sender, EventArgs e)
        {
            txtDiaChi.Text = "";
            txtEmail.Text = "";
            txtSDT.Text = "";
            txtTenGV.Text = "";
            cbxKhoi.Text = "";
            cbxMonHoc.Text = "";
            dataGridView1.Enabled = true;
        }

        private void showKhoi()
        {
            tbl_Khoi tbl_Khoi = (tbl_Khoi)cbxKhoi.SelectedItem;
            cbxKhoi.DataSource = gvBUO.getKhoi();
            cbxKhoi.DisplayMember = "TenKhoi";
            cbxKhoi.ValueMember = "id_Khoi";
        }

        private void showMonDay()
        {
            tbl_MonHoc tbl_MonHoc = (tbl_MonHoc)cbxMonHoc.SelectedItem;
            cbxMonHoc.DataSource = gvBUO.getMonDay(cbxKhoi.SelectedValue.ToString());
            cbxMonHoc.DisplayMember = "TenMonHoc";
            cbxMonHoc.ValueMember = "id_MonHoc";
        }

        private void cbxKhoi_SelectedIndexChanged(object sender, EventArgs e)
        {
            showMonDay();
            cbxMonHoc.Text = mhBUO.getMonHoc(md);
        }

        private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
        {
            try
            {
                if (e.Button == System.Windows.Forms.MouseButtons.Right)
                {

                    DataGridView.HitTestInfo hit = dataGridView1.HitTest(e.X, e.Y);


                    // Only RowHeader and Cells (which make up a row) should fire a menu
                    if (hit.Type == DataGridViewHitTestType.RowHeader
                        || hit.Type == DataGridViewHitTestType.Cell
                        && hit.RowIndex >= 0)
                    {
                        // Notice how I assign the menu to the dataGridView below...

                        dataGridView1.ClearSelection();
                        dataGridView1.Rows[hit.RowIndex].Selected = true;
                        dataGridView1.ContextMenuStrip = contextMenuStrip1;
                        dataGridView1.ContextMenuStrip.Show(this.dataGridView1, new Point(e.X, e.Y));

                        id = dataGridView1.HitTest(e.X, e.Y).RowIndex;
                        id_GV = dataGridView1.Rows[id].Cells[0].Value.ToString();

                    }
                    // and then I remove the menu here...
                    // This is because after we assign it to the dataGridView
                    // it allows you to right-click anywhere on the control to
                    // get a menu.  Try removing this line and expirement.
                    dataGridView1.ContextMenuStrip = null;

                }
            }
            catch
            {
                contextMenuStrip1.Visible = false;
            }
        }

        private void danhSáchGiáoViênToolStripMenuItem_Click(object sender, EventArgs e)
        {
            UCDanhSachGV a = new UCDanhSachGV();

            Form form = this.ParentForm;
            ControlAccess.ShowConTrol(form, a, "grbHienThiChucNang");
        }

        private void thêmMớiGiáoViênToolStripMenuItem_Click(object sender, EventArgs e)
        {
            UCThemGiaoVien a = new UCThemGiaoVien();

            Form form = this.ParentForm;
            ControlAccess.ShowConTrol(form, a, "grbHienThiChucNang");
        }

        private void xóaGiáoViênToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult ds = MessageBox.Show("Bạn có chắc chắn muốn xóa", "Xóa dữ liệu giáo viên", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (ds == DialogResult.Yes)
            {
                if (gvBUO.Delete(id_GV))
                {
                    MessageBox.Show("Xóa giáo viên thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("Xóa giáo viên thất bại", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                dataGridView1.DataSource = gvBUO.HienThiGiaoVien();
            }
        }

        private void btnCapNhat_Click(object sender, EventArgs e)
        {
            if ((txtDiaChi.Text == "") || (txtEmail.Text == "") || (txtSDT.Text == "") || (txtTenGV.Text == "") || (cbxKhoi.Text == "") || (cbxMonHoc.Text == ""))
            {
                MessageBox.Show("Chưa nhập đủ thông tin", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                if (numberBUO.isNumberInput(txtSDT.Text) == false)
                {
                    MessageBox.Show("Trường số điện thoại phải nhập số", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    //String a = gvBUO.getIDMonHoc(cbxKhoi.SelectedValue.ToString(), cbxMonHoc.Text);

                    String idKh, tenmon;
                    idKh = cbxKhoi.SelectedValue.ToString();
                    tenmon = cbxMonHoc.Text;

                    String idMon = mhBUO.getIDMon(idKh, tenmon);

                    tbl_GiaoVien gv = new tbl_GiaoVien();
                    gv.id_MonHoc = idMon;
                    gv.HoTenGV = txtTenGV.Text;
                    gv.SDT = txtSDT.Text;
                    gv.Email = txtEmail.Text;
                    gv.DiaChi = txtDiaChi.Text;
                    gv.NgaySinh = dateTimePicker1.Text;

                    if (gvBUO.Update(lbl_idGV.Text, gv) == true)
                    {
                        dataGridView1.AutoGenerateColumns = false;
                        dataGridView1.DataSource = gvBUO.HienThiGiaoVien();
                        MessageBox.Show("Cập nhật thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        MessageBox.Show("Cập nhật thất bại", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                }
            }
        }

        public void binding()
        {
            lbl_idGV.DataBindings.Clear();
            lbl_idGV.DataBindings.Add("Text", dataGridView1.DataSource, "id_GiaoVien");
            txtDiaChi.DataBindings.Clear();
            txtDiaChi.DataBindings.Add("Text", dataGridView1.DataSource, "DiaChi");
            txtEmail.DataBindings.Clear();
            txtEmail.DataBindings.Add("Text", dataGridView1.DataSource, "Email");
            txtSDT.DataBindings.Clear();
            txtSDT.DataBindings.Add("Text", dataGridView1.DataSource, "SDT");
            txtTenGV.DataBindings.Clear();
            txtTenGV.DataBindings.Add("Text", dataGridView1.DataSource, "HoTenGV");
            dateTimePicker1.DataBindings.Clear();
            dateTimePicker1.DataBindings.Add("Text", dataGridView1.DataSource, "NgaySinh");
            cbxKhoi.DataBindings.Clear();
            cbxKhoi.DataBindings.Add("Text", dataGridView1.DataSource, "TenKhoi");
            cbxMonHoc.DataBindings.Clear();
            cbxMonHoc.DataBindings.Add("Text", dataGridView1.DataSource, "TenMonHoc");
        }
    }
}
